<?php
require_once("database.php");
class xl_san_pham extends database {
	function danh_sach($vi_tri,$so_luong) {
		$vi_tri=intval($vi_tri);
		$so_luong=intval($so_luong);
		$sql="select * from san_pham where trang_thai=1 limit $vi_tri,$so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_admin($vi_tri,$so_luong) {
		$vi_tri=intval($vi_tri);
		$so_luong=intval($so_luong);
		$sql="select * from san_pham limit $vi_tri,$so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_ban_chay() {
		$sql="select * from (
					select sp.*,b2.so_luong from san_pham sp,(
						select * from (
							SELECT count(*) as so_luong,ma_san_pham from chi_tiet_hoa_don group by ma_san_pham) as bang) as b2 where b2.ma_san_pham=sp.ma) as tb where trang_thai=1 order by so_luong desc limit 0,9";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_moi() {
		$sql="select * from san_pham where san_pham_moi=1 and trang_thai=1 ORDER BY ngay_tao DESC limit 0,9";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_xem_nhieu() {
		$sql="select * from san_pham where trang_thai=1 ORDER BY so_lan_xem DESC limit 0,9";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_theo_loai($ma_loai,$vi_tri,$so_luong) {
		$ma_loai=intval($ma_loai);
		$vi_tri=intval($vi_tri);
		$so_luong=intval($so_luong);
		$sql="select * from san_pham where ma_loai=$ma_loai and trang_thai=1 limit $vi_tri,$so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_cung_loai($ma_loai,$ma) {
		$ma_loai=intval($ma_loai);
		$ma=intval($ma);
		$sql="select * from san_pham where ma_loai=$ma_loai and ma!=$ma and trang_thai=1 LIMIT 0,6";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function so_luong_theo_ma($ma_loai) {
		$ma_loai=intval($ma_loai);
		$sql="select count(*) from san_pham where ma_loai=$ma_loai and trang_thai=1";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function so_luong() {
		$sql="select count(*) from san_pham where trang_thai=1";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function so_luong_admin() {
		$sql="select count(*) from san_pham";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function xem($ma) {
		$ma=intval($ma);
		$sql="select * from san_pham where ma='$ma' and trang_thai=1";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xoa($ma) {
		$ma=intval($ma);

		$sql="delete from san_pham where ma='$ma'";
		$this->setQuery($sql);
		$this->query();
	}
	function them($ten,$ma_loai,$tac_gia,$nha_xuat_ban,$don_gia,$hinh,$san_pham_moi,$so_lan_xem,$ngay_tao,$trang_thai) {
		$ten=addslashes($ten);
		$ma_loai=intval($ma_loai);
		$tac_gia=addslashes($tac_gia);
		$nha_xuat_ban=addslashes($nha_xuat_ban);
		$hinh=addslashes($hinh);
		$so_lan_xem=intval($so_lan_xem);
		$sql="INSERT INTO san_pham(ten,ma_loai,tac_gia,nha_xuat_ban,don_gia,hinh,san_pham_moi,so_lan_xem,ngay_tao,trang_thai) VALUES ('$ten','$ma_loai','$tac_gia','$nha_xuat_ban','$don_gia','$hinh','$san_pham_moi','$so_lan_xem',now(),'$trang_thai')";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat($ma,$ten,$ma_loai,$tac_gia,$nha_xuat_ban,$don_gia,$hinh,$san_pham_moi,$so_lan_xem,$ngay_tao,$trang_thai) {
		$ma=intval($ma);
		$ten=addslashes($ten);
		$ma_loai=intval($ma_loai);
		$tac_gia=addslashes($tac_gia);
		$nha_xuat_ban=addslashes($nha_xuat_ban);
		$hinh=addslashes($hinh);
		$so_lan_xem=intval($so_lan_xem);
		if($hinh==NULL) {
			$sql="UPDATE san_pham SET ten='$ten',ma_loai='$ma_loai',tac_gia='$tac_gia',nha_xuat_ban='$nha_xuat_ban', don_gia='$don_gia',san_pham_moi='$san_pham_moi',so_lan_xem='$so_lan_xem',ngay_tao='$ngay_tao' WHERE ma='$ma'";
		}
		else {
			$sql="UPDATE san_pham SET ten='$ten',ma_loai='$ma_loai',tac_gia='$tac_gia',nha_xuat_ban='$nha_xuat_ban', don_gia='$don_gia',hinh='$hinh',san_pham_moi='$san_pham_moi',so_lan_xem='$so_lan_xem',ngay_tao='$ngay_tao' WHERE ma='$ma'";
		}
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat_lan_xem($ma) {
		$ma=intval($ma);
		$sql="UPDATE san_pham SET so_lan_xem=so_lan_xem+1 WHERE ma='$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat_tt($ma) {
		$ma=intval($ma);
		$sql="UPDATE san_pham SET trang_thai=1-trang_thai WHERE ma='$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat_sp_moi($ma) {
		$ma=intval($ma);
		$sql="UPDATE san_pham SET san_pham_moi=1-san_pham_moi WHERE ma='$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function tim_kiem($tu_khoa,$vi_tri,$so_luong) {
		$sql="select * from san_pham where (ten like '%$tu_khoa%' or tac_gia like '%$tu_khoa%' or nha_xuat_ban like '%$tu_khoa%' or hinh like '%$tu_khoa%') and trang_thai=1 ORDER BY so_lan_xem DESC limit $vi_tri,$so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();	
	}
	function so_luong_theo_tu_khoa($tu_khoa) {
		$sql="select count(*) from san_pham where (ten like '%$tu_khoa%' or tac_gia like '%$tu_khoa%' or nha_xuat_ban like '%$tu_khoa%' or hinh like '%$tu_khoa%') and trang_thai=1"; 
		$this->setQuery($sql);
		return $this->loadResult();	
	}
 }
?>